package com.tal.live.moudels.trc.helper;

import com.arronlong.httpclientutil.common.HttpHeader;
import com.google.gson.Gson;
import com.tal.live.pojo.dto.DocumentQuery;
import com.tal.live.pojo.dto.QueryTicket;
import com.tal.live.utils.http.HttpClientHoler;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * <Description>
 *
 * @author xianliru@100tal.com
 * @version 1.0
 * @createDate 2019/10/14 16:11
 */
@Component
@Slf4j
public class TrcHttpHelper {

    @Value("${transfer.stc.paperrest}")
    private String stc;

    @Value("${transfer.stc.jydriverest}")
    private String jydriverest;

    //获取访问ticket
    public static final String QUERY_TICKET_URL ="/paperrest/rest/jyCloud/queryTicket.json";

    //根据课件id查询编辑地址
    public static final String COURSE_WARE_EDIT_URL = "/paperrest/rest/courseWare/getCloudCourseWareEditUrl.json";

    //获取个人云盘访问地址
    public static final String QUERY_STORAGE_INDEX_URL = "/paperrest/rest/jyCloud/queryStorageIndexUrl.json";

    //个人文档rest
    public static final String QUERY_DOCUMENT_BY_PAGE = "/rest/doc/queryDocumentByPage";

    public static final Gson GSON = new Gson();

    /**
     * 获取访问ticket
     * @param ticket
     * @return
     */
    public String queryTicket(QueryTicket ticket){
        Header[] headers = HttpHeader.custom()
                .contentType("application/json")
                .build();
        String result = HttpClientHoler.post(stc+QUERY_TICKET_URL, null, headers, GSON.toJson(ticket));
        return result;
    }

    /**
     *  查询编辑地址
     * @param id 课件ID
     * @param ticket
     * @return
     */
    public String getCourseWareEditUrl(String id,String ticket){
        String result = HttpClientHoler.get(stc + COURSE_WARE_EDIT_URL+"?id="+id+"&ticket="+ticket,null);
        return result;
    }

    /**
     * 个人云盘访问地址
     * @param ticket
     * @return
     */
    public String getQueryStorageIndexUrl(String ticket){
        Header[] headers = HttpHeader.custom()
                .contentType("application/json")
                .build();
        Map<String,Object> map = new HashMap<>();
        map.put("ticket",ticket);
        String result = HttpClientHoler.post(stc+QUERY_STORAGE_INDEX_URL, null, headers, GSON.toJson(map));

        return result;
    }

    /**
     * 个人文档rest列表
     * @param documentQuery
     * @return
     */
    public String queryDocumentByPage(DocumentQuery documentQuery){
        Header[] headers = HttpHeader.custom()
                .contentType("application/json")
                .build();
        String result = HttpClientHoler.post(jydriverest+QUERY_DOCUMENT_BY_PAGE, null, headers, GSON.toJson(documentQuery));
        return result;
    }
}
